<?php
include("db.inc.php");

$names = Array();
$r = db_query("SELECT * FROM user");
while(($v = mysql_fetch_array($r)) != NULL) {
 $names[] = $v["name"];
}

$jahrgaenge = Array(
"2008/09",
"2009/10",
"2010/11");

$names = array_merge($names, $jahrgaenge);

$list_limit = 10; // maximum number of entries displayed

$q = trim(urldecode($_POST["q"]));
$typ = trim(urldecode($_POST["typ"]));
$f = trim(urldecode($_POST["f"]));

if($q == "") exit;

if($q == "*") $list_limit = 65536;

$cnt = 0;
$cnts = 0;
$lb = "";
$totmatch = 0;

$list = array();
if($typ == "jahrgang") $list = $names;

for($i = 0; $i < count($list); $i++) {
 if((($pos = stripos($list[$i], $q)) !== false) || $q == "*") {
  $cnt++;
  if($cnts >= $list_limit) continue;
  $cnts++;
  $lb = $list[$i]; // remember last entry
  if(strtolower($q) == strtolower($list[$i])) $totmatch = 1;
  // entry matches, return it
  $r = "<div class=\"search_bandname\" onClick=\"set('".$f."', '".$list[$i]."');\" onmouseover=\"this.className='search_bandname_hover';\" onmouseout=\"this.className='search_bandname';\">";
  $r.= substr($list[$i], 0, $pos);
  $r.="<span class=\"search_bandname_hi\">";
  $r.= substr($list[$i], $pos, strlen($q));
  $r.="</span>";
  $r.= substr($list[$i], $pos+strlen($q));
  echo $r."</div>";
 }

}

if($cnt > $cnts) echo "<div class=\"search_morebands\">".($cnt-$cnts)." weitere".(($cnt-$cnts) == 1 ? "r" : "")." Treffer...</div>";

// if only 1 bandname left, return it
if($cnt == 1) echo "<input type=\"hidden\" id=\"search_bandleft\" value=\"".$lb."\" />";
// return if input is a valid band, or if it just matches a band
echo "<input type=\"hidden\" id=\"search_totmatch\" value=\"".$totmatch."\" />";
// return number of matches
echo "<input type=\"hidden\" id=\"search_matches\" value=\"".$cnt."\" />";

?>